home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / boopsi / demo4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  10.7 KB  |  421 lines

  1. /* demo4.c -- demonstration of         :ts=8
  2.  * Basic Object Oriented Programming System for Intuition
  3.  *
  4.  * This demo shows the construction of a composite (group)
  5.  * gadget, collecting all four gadgets used in the
  6.  * other demos.
  7.  *
  8.  * The application can use this gadget as a single entity
  9.  * with either the GADGETUP or IDCMPUPDATE messages.
  10.  */
  11.  
  12. /*
  13. Copyright (c) 1989 Commodore-Amiga, Inc.
  14.  
  15. Executables based on this information may be used in software
  16. for Commodore Amiga computers. All other rights reserved.
  17. This information is provided "as is"; no warranties are made.
  18. All use is at your own risk, and no liability or responsibility
  19. is assumed.
  20. */
  21.  
  22. #include "sysall.h"
  23. #include <intuition/icclass.h>
  24. #include "mymodel.h"
  25.  
  26. #define D(x)    x
  27.  
  28. struct  IntuitionBase   *IntuitionBase;
  29. struct  GfxBase         *GfxBase;
  30. struct  Library         *UtilityBase;
  31.  
  32. /* from varargs.c -- interface to NewObjectA()    */
  33. Object    *NewObject();
  34.  
  35. ULONG    myiflags =  CLOSEWINDOW | GADGETUP | IDCMPUPDATE;
  36. struct Window    *w = NULL;
  37.  
  38. Object        *mymodel = NULL; /* the repository of the "Current Value" */
  39. void    *MyModClass = NULL;
  40. void    *initMyModClass();
  41.  
  42.  
  43. struct Gadget    *groupg = NULL;
  44.  
  45. /* pictures for arrows    */
  46. struct Image    *upimage = NULL;
  47. struct Image    *downimage = NULL;
  48.  
  49. #define PWIDTH        (120)    /* width of horizontal propslider    */
  50. #define SWIDTH        (50)
  51.  
  52. #define PROPRANGE    (20)
  53. #define INITVAL        (0)    /* initial value of string and slider    */
  54.  
  55. enum gadgetids {
  56.     gUp = 1,
  57.     gDown,
  58.     gSlider,
  59.     gString,
  60.     gGroup,
  61. };
  62.  
  63. /* a macro for the "adjacent" position to the right of a gadget,
  64.  * but safe, if the reference gadget is NULL
  65.  */
  66. #define RIGHTBY( g )    ( ((g)==NULL)? 20: ((g)->LeftEdge + (g)->Width ) )
  67.  
  68. /****************************************************************/
  69. /*  mapping tag lists                        */
  70. /****************************************************************/
  71.  
  72. /* for IDCMPUPDATE    */
  73. struct TagItem    model2me[] = {
  74.     {MYMODA_CURRVAL, ICSPECIAL_CODE },
  75.     /* put (16 bits of) currval into IntuiMessage.Code    */
  76.     { TAG_END, }
  77. };
  78.  
  79. struct TagItem    slider2model[] = {
  80.     {PGA_TOP, MYMODA_CURRVAL},
  81.     {TAG_END, }
  82. };
  83.  
  84. struct TagItem    model2slider[] = {
  85.     {MYMODA_CURRVAL, PGA_TOP},
  86.     {MYMODA_RANGE, PGA_TOTAL },
  87.     {TAG_END, }
  88. };
  89.  
  90. struct TagItem    string2model[] = {
  91.     {STRINGA_LongVal, MYMODA_CURRVAL},
  92.     {TAG_END, }
  93. };
  94.  
  95. struct TagItem    model2string[] = {
  96.     {MYMODA_CURRVAL, STRINGA_LongVal},
  97.     {TAG_END, }
  98. };
  99.  
  100. struct TagItem    uparrow2model[] = {
  101.     {GA_ID, MYMODA_INCRSTROBE},
  102.     {TAG_END, }
  103. };
  104.  
  105. struct TagItem    downarrow2model[] = {
  106.     {GA_ID, MYMODA_DECRSTROBE},
  107.     {TAG_END, }
  108. };
  109.  
  110. /****************************************************************/
  111. /* tag lists for creating objects                */
  112. /****************************************************************/
  113.  
  114.  
  115. struct TagItem    proptags[] = {
  116.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  117.  
  118.     /* {ICA_TARGET,    XXX }, * will be model object    */
  119.     {ICA_MAP,        (ULONG) &slider2model[0]},
  120.  
  121.     {PGA_FREEDOM,    FREEHORIZ},
  122.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  123.  
  124. #if 0    /* the whole set of gadgets will be initialized together    */
  125.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  126.     {PGA_TOTAL,        PROPRANGE},
  127. #endif
  128.  
  129.     {TAG_END ,}
  130. };
  131.  
  132. struct TagItem    stringtags[] = {
  133.     {GA_WIDTH,        SWIDTH},
  134.     {GA_HEIGHT,        12},        /* fix this    */
  135.  
  136.     /* {ICA_TARGET,    XXXX },        will be model object    */
  137.     {ICA_MAP,        (ULONG) &string2model[0]},
  138.  
  139.     {STRINGA_MaxChars,    10},
  140.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  141.     {STRINGA_Justification,
  142.                 STRINGRIGHT},
  143.     {TAG_END, }
  144. };
  145.  
  146. #define MYWINDOWTITLE    "boopsi Demo 4"
  147.  
  148. main()
  149. {
  150.     struct DrawInfo    *GetScreenDrawInfo();
  151.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  152.  
  153.     struct Gadget    *g;
  154.     struct DrawInfo    *drinfo;
  155.  
  156.     WORD        nextleft;
  157.  
  158.     Object        *ic;
  159.  
  160.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  161.     { cleanup("no V36 utility library\n"); }
  162.  
  163.     if (!(IntuitionBase = 
  164.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  165.     { cleanup("no V36 intuition library\n"); }
  166.  
  167.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  168.     { cleanup("no V36 graphics library\n"); }
  169.  
  170.     w = OpenWindowTags( NULL, 
  171.         WA_Title,    MYWINDOWTITLE,
  172.         WA_SimpleRefresh, TRUE,
  173.         WA_NoCareRefresh, TRUE,
  174.         WA_DepthGadget,    TRUE,
  175.         WA_DragBar,    TRUE,
  176.         WA_Left,    300,
  177.         WA_Top,        50,
  178.         WA_Width,    280,
  179.         WA_Height,    120,
  180.         WA_IDCMP,    myiflags,
  181.         WA_CloseGadget,    TRUE,
  182.             TAG_END );
  183.     D( printf("window at %lx\n", w ) );
  184.  
  185.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  186.     drinfo = GetScreenDrawInfo( w->WScreen );
  187.  
  188.     /* get images for the up and down arrows, sensitive
  189.      * to depth and pen specs for current screen (we'll be
  190.      * adding resolution/size selection later).
  191.      */
  192.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  193.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  194.     SYSIA_DrawInfo, drinfo,
  195.     SYSIA_Which,    UPIMAGE,
  196.         TAG_END );
  197.  
  198.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  199.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  200.     SYSIA_Which,    DOWNIMAGE,
  201.     SYSIA_DrawInfo, drinfo,
  202.         TAG_END );
  203.  
  204.     D( printf("get group 'master' gadget\n"));
  205.  
  206.     /* get a group gadget which will manage our four basic gadgets */
  207.     groupg = (struct Gadget *) NewObject( NULL, GROUPGCLASS,
  208.                 GA_LEFT,    w->BorderLeft,
  209.                 GA_TOP,        w->BorderTop,
  210.                 GA_ID,        gGroup,
  211.                     TAG_END );
  212.     if ( ! groupg ) cleanup( "can't get group gadget" );
  213.     D( printf("group gadget at %lx\n", groupg ) );
  214.  
  215.     /* get "model" object which is the repository of our "current
  216.      * value" and is the hub of object interconnection.
  217.      * This thing also is used to free icclass objects,
  218.      * so we'd better make sure it got allocated.
  219.      */
  220.     MyModClass = initMyModClass();    /* private class    */
  221.     D( printf("get model object, class at %lx\n", MyModClass ) );
  222.  
  223.     /* We connect the model directly to ourselves, the
  224.      * group gadget isn't in the loop.
  225.      */
  226.     mymodel = (Object *) NewObject( MyModClass, NULL, 
  227.             ICA_TARGET,    ICTARGET_IDCMP,
  228.             ICA_MAP,    model2me,
  229.             TAG_END );
  230.     D( printf("model at %lx\n", mymodel ) );
  231.  
  232.     if ( ! mymodel ) cleanup( "couldn't get model object" );
  233.  
  234.     /* get the string gadget    */
  235.     g  = (struct Gadget *) NewObject( NULL, "strgclass",
  236.         GA_LEFT,    0,
  237.     ICA_TARGET,    mymodel,
  238.     TAG_MORE,    stringtags,
  239.     TAG_END );
  240.     D( printf( "string gadget at %lx\n", g ) );
  241.  
  242.     /* when I 'addmember' this to the group, 
  243.      * the group willoffset its position, so
  244.      * if I want to use it, it has to be now
  245.      */
  246.     nextleft = RIGHTBY( g );
  247.  
  248.     /* add it to the group ...    */
  249.     DoMethod( groupg, OM_ADDMEMBER, g );
  250.  
  251.     /* The string gadget now talks to the model.
  252.      * Add an interconnection from the model to
  253.      * the string gadget.
  254.      */
  255.     ic = NewObject( NULL, ICCLASS,
  256.             ICA_TARGET,    g,
  257.         ICA_MAP,    model2string,
  258.         TAG_END );
  259.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  260.  
  261.     /* get the prop gadget, immediately to the right of the string */
  262.     g = (struct Gadget *) NewObject( NULL, "propgclass",
  263.     GA_LEFT,    nextleft,
  264.     GA_HEIGHT,    downimage? downimage->Height: 20,
  265.     ICA_TARGET,    mymodel,
  266.     TAG_MORE,    proptags,
  267.     TAG_END );
  268.     D( printf( "prop gadget returned: %lx\n", g ) );
  269.     nextleft = RIGHTBY( g );
  270.  
  271.     /* add it to the group ...    */
  272.     DoMethod( groupg, OM_ADDMEMBER, g );
  273.  
  274.     /* ... and get an ic for the prop gadget    */
  275.     ic = NewObject( NULL, ICCLASS,
  276.             ICA_TARGET,    g,
  277.         ICA_MAP,    model2slider,
  278.         TAG_END );
  279.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  280.  
  281.     /* down button is immediately to the right ...    */
  282.     g = (struct Gadget *) NewObject( NULL, "buttongclass",
  283.     GA_IMAGE,     downimage,
  284.     GA_LEFT,    nextleft,
  285.     GA_ID,        gDown,
  286.     /* interconnections ...    */
  287.     ICA_TARGET,    mymodel,
  288.     ICA_MAP,    &downarrow2model[0],
  289.     TAG_END );
  290.     D( printf("downgadget at %lx\n", g ));
  291.     nextleft = RIGHTBY( g );
  292.  
  293.     /* ... and add it to the group gadget    */
  294.     DoMethod( groupg, OM_ADDMEMBER, g );
  295.  
  296.     /* the buttons don't need to hear from the model, 
  297.      * hence, no ic's for them.
  298.      */
  299.  
  300.     /* up arrow button    */
  301.     g = (struct Gadget *) NewObject( NULL, "buttongclass",
  302.     GA_IMAGE,     upimage,
  303.     GA_LEFT,    nextleft,
  304.     GA_ID,        gUp,
  305.     /* interconnections ...    */
  306.     ICA_MAP,    &uparrow2model[0],
  307.     ICA_TARGET,    mymodel,
  308.     TAG_END );
  309.     D( printf("upgadget at %lx\n", g ));
  310.  
  311.     /* ... and add it to the group gadget    */
  312.     DoMethod( groupg, OM_ADDMEMBER, g );
  313.  
  314.     D(printf("all objects initialized\n"));
  315.  
  316.     /* As a variation on demo3, we'll initialize the
  317.      * values of everything before we add it to the window,
  318.      * and we can use the more vanilla SetAttrs() function.
  319.      *
  320.      * I can't call SetGadgetAttrs() for 'groupg' and have
  321.      * it propagate, since the group gadget doesn't know
  322.      * anything about the model, in this example.
  323.      */
  324.      SetAttrs( mymodel, 
  325.         MYMODA_RANGE, PROPRANGE,
  326.         MYMODA_CURRVAL, PROPRANGE/2,
  327.         TAG_END );
  328.  
  329.     /* check out the positioning of a group gadget */
  330.     SetGadgetAttrs( groupg, w, NULL,
  331.             GA_LEFT, 4 * w->BorderLeft,
  332.         GA_TOP,     3 * w->BorderTop,
  333.         TAG_END );
  334.  
  335.     /* And here's the nice part:
  336.      * you add just the ONE gadget
  337.      * to the window.
  338.      */
  339.     AddGList( w, groupg, -1, 1, NULL );
  340.     RefreshGList( groupg, w, NULL, -1 );
  341.  
  342.     D( printf("gadgets added and refreshed \n") );
  343.  
  344.     goHandleWindow( w );
  345.  
  346.     RemoveGList( w, groupg, 1 );    /* just one    */
  347.  
  348.     FreeScreenDrawInfo( w->WScreen, drinfo );
  349.     cleanup( "all done" );
  350. }
  351.  
  352. cleanup( str )
  353. char    *str;
  354. {
  355.     if (str) printf("%s\n", str);
  356.  
  357.     D( printf("dispose objects\n") );
  358.     DisposeObject( mymodel );
  359.  
  360.     /* and more fun part: you toss this away, and all associated
  361.      * gadget components go with it.
  362.      */
  363.     DisposeObject( groupg );
  364.  
  365.     DisposeObject( upimage);
  366.     DisposeObject( downimage);
  367.     D( printf("have disposed.\n") );
  368.  
  369.     if ( MyModClass ) freeMyModClass( MyModClass );
  370.  
  371.     if ( w ) CloseWindow( w );
  372.  
  373.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  374.     if (GfxBase) CloseLibrary(GfxBase);
  375.     if (UtilityBase) CloseLibrary(UtilityBase);
  376.  
  377.     exit (0);
  378. }
  379.  
  380. goHandleWindow( w )
  381. struct Window    *w;
  382. {
  383.     struct TagItem    *FindTagItem();
  384.  
  385.     struct IntuiMessage *imsg;
  386.     struct TagItem    *tags;
  387.     ULONG        currval;
  388.     /* struct TagItem    *ti; */
  389.  
  390.     for(;;)
  391.     {
  392.     WaitPort( w->UserPort );
  393.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  394.     {
  395.         switch ( imsg->Class )
  396.         {
  397.         case CLOSEWINDOW:
  398.             ReplyMsg( (struct Message *) imsg );
  399.         return;
  400.  
  401.        case GADGETUP:
  402.            D( printf("GADGETUP GadgetID: %lx\n",
  403.             ((struct Gadget *) imsg->IAddress)->GadgetID ) );
  404.         break;
  405.  
  406.         case IDCMPUPDATE:
  407.         tags = (struct TagItem *) imsg->IAddress;
  408.             D( printf("IDCMPUPDATE, quals %lx code (decimal) %ld\n",
  409.             imsg->Qualifier, imsg->Code ));
  410.  
  411.         GetAttr( MYMODA_CURRVAL, mymodel, &currval );
  412.         D( printf("Current value now %ld\n", currval ) );
  413.  
  414.         break;
  415.         }
  416.         ReplyMsg( (struct Message *) imsg );
  417.     }
  418.     }
  419. }
  420.  
  421.